package com.hzsparrow.ai.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzsparrow.ai.entity.Conversation;
import com.hzsparrow.ai.mapper.ConversationMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

/**
 * 对话服务实现类
 */
@Service
public class ConversationServiceImpl extends ServiceImpl<ConversationMapper, Conversation> implements ConversationService {

    @Override
    public Conversation createConversation(Conversation conversation) {
        // 设置创建和更新时间
        LocalDateTime now = LocalDateTime.now();
        conversation.setCreateTime(now);
        conversation.setUpdateTime(now);
        
        // 生成会话ID如果不存在
        if (conversation.getSessionId() == null || conversation.getSessionId().isEmpty()) {
            conversation.setSessionId(UUID.randomUUID().toString());
        }
        
        // 保存对话
        this.save(conversation);
        return conversation;
    }

    @Override
    public Conversation getConversation(Long id) {
        return this.getById(id);
    }

    @Override
    public Conversation getConversationBySessionId(String sessionId) {
        LambdaQueryWrapper<Conversation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Conversation::getSessionId, sessionId);
        return this.getOne(queryWrapper);
    }

    @Override
    public Conversation updateConversation(Conversation conversation) {
        // 设置更新时间
        conversation.setUpdateTime(LocalDateTime.now());
        
        // 更新对话
        this.updateById(conversation);
        return conversation;
    }

    @Override
    public boolean deleteConversation(Long id) {
        return this.removeById(id);
    }

    @Override
    public List<Conversation> getUserConversations(Long userId) {
        LambdaQueryWrapper<Conversation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Conversation::getUserId, userId)
                   .orderByDesc(Conversation::getUpdateTime);
        return this.list(queryWrapper);
    }

    @Override
    public Page<Conversation> listConversations(int page, int size, Long userId) {
        Page<Conversation> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<Conversation> queryWrapper = new LambdaQueryWrapper<>();
        
        // 如果指定了用户ID，则查询该用户的对话
        if (userId != null) {
            queryWrapper.eq(Conversation::getUserId, userId);
        }
        
        queryWrapper.orderByDesc(Conversation::getUpdateTime);
        return this.page(pageParam, queryWrapper);
    }

    @Override
    public Conversation updateConversationTitle(String sessionId, String title) {
        Conversation conversation = getConversationBySessionId(sessionId);
        if (conversation != null) {
            conversation.setTitle(title);
            conversation.setUpdateTime(LocalDateTime.now());
            this.updateById(conversation);
        }
        return conversation;
    }
} 